<template>
  <!-- 会员 -->
  <section>
    <table border="0" width="100%" style="border-spacing: 0;border-collapse: collapse;">
      <tr>
        <td>
          <el-form :inline="true">
            <el-form-item>
              <el-input v-model="form.nameOrMobile" clearable placeholder="患者姓名/手机号后4位"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" circle icon="el-icon-search" @click="getData(-1)"></el-button>
            </el-form-item>
            <!-- <el-form-item>
              <el-button type="primary" :disabled="!qxBtns.add" plain @click="openEdit(null)">+添加</el-button>
            </el-form-item>-->
          </el-form>
        </td>
        <td style="text-align:right;vertical-align: top;">
          <el-button @click="exportExcel()" :disabled="!qxBtns.vipexport" plain>导出</el-button>
        </td>
      </tr>
    </table>
    <table class="table-box">
      <thead>
        <tr>
          <th>姓名</th>
          <th>手机号</th>
          <th>会员类型</th>
          <th>累积消费</th>
          <th>储值金额</th>
          <th>累计储值</th>
          <th>到期日期</th>
          <th>积分</th>
          <th>状态</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td>
            <el-button type="text" @click="getDetail(item)">{{item.name}}</el-button>
          </td>
          <td>{{item.mobile}}</td>
          <td>{{item.levelName}}</td>
          <td>{{item.theCcumulative}}</td>
          <td>{{item.patientMoney}}</td>
          <td>{{item.storedValue}}</td>
          <td>{{item.levelTime}}</td>
          <td>{{item.patientScore}}</td>
          <td>{{item.pstatus}}</td>
          <td>
            <el-link v-if="qxBtns.update" :underline="false" @click="onJoin(item)" type="primary">充值</el-link>&nbsp;
            <!-- <el-link v-if="qxBtns.join" type="primary" @click="onJoin(item)">关联</el-link>&nbsp; -->
            <el-link v-if="qxBtns.update" :underline="false" @click="openRefund(item)" type="danger">退款</el-link>&nbsp;
            <el-link v-if="qxBtns.update" :underline="false" @click="setVip(item)" type="primary">设置会员</el-link>
          </td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <set v-model="dialogEdit" v-if="dialogEdit" :curItem="curItem" @success="getData"></set>
    <pay v-model="dialogJoin" v-if="dialogJoin" :curItem="curItem" @success="getData"></pay>
    <refund v-model="dialogRefund" v-if="dialogRefund" :curItem="curItem" @success="getData"></refund>
  </section>
</template>
<script>
import Excel from "@/_js/excel.js";
import { debuglog } from "util";
export default {
  name: "patientList",
  data() {
    return {
      dialogJoin: false,
      dialogEdit: false,
      dialogRefund: false,
      curItem: null,
      pager: { pageSize: 10, pageNum: 1, total: 0 },
      tableList: [],
      form: {
        hpId: "",
        nameOrMobile: "",
        type: 2,
        archivesOrMedical: "",
        birthday: ""
      },
      treeList: [],
      qxBtns: {},
      defaultProps: {
        children: "child",
        label: "drugTypeName"
      },
      dataList: [],
      distype: "2",
      setType: "1"
    };
  },
  created() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    th.getQX();
    th.getData();
  },
  methods: {
    onDel(obj, index) {
      let th = this;
      th.$confirm(`确定删除吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          let j = { pId: obj.pId };
          th.showLoading();
          th.$api.deletePatient(j).then(res => {
            if (res.executed) {
              th.$message.success("删除成功");
              th.getData();
            } else {
              th.$message.error(res.message);
            }
            th.showLoading(false);
          });
        })
        .catch(() => {});
    },
    handleCommand(command, item, index) {
      let th = this;
      if (command == "del") {
        th.onDel();
      }
    },
    getData(type, id) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      if (id != undefined) {
        if (id == -1) {
          th.form.drugTypeId = "";
        } else {
          th.form.drugTypeId = id;
        }
      }
      let j = {};
      Object.assign(j, th.pager);
      Object.assign(j, th.form);
      th.showLoading();
      th.$api.selectLevelEnumList(j).then(res => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            th.tableList = res.dataBody.list;
            th.pager.total = res.dataBody.total;
          } else {
            th.tableList = [];
            th.pager.total = 0;
          }
        }
        th.showLoading(false);
      });
    },
    setVip(obj) {
      let th = this;
      th.curItem = obj;
      th.dialogEdit = true;
    },
    //充值
    onJoin(obj) {
      this.dialogJoin = true;
      this.curItem = obj;
    },
    //退款
    openRefund(item) {
      let th = this;
      th.dialogRefund = true;
      th.curItem = item;
    },
    openEdit(obj) {
      let th = this;
      let j = {};
      if (obj != null) {
        j = {
          id: th.jiaMi(obj.id)
        };
      } else {
        j = {
          id: th.jiaMi(0)
        };
      }
      th.$router.push({
        path: "/page/addPatient",
        query: th.getUrlToken(j)
      });
    },
    openSet(type) {
      let th = this;
      th.dialogEdit = true;
      th.setType = type;
    },
    //修改药品状态
    updateMedicalDrugStatus(obj) {
      let th = this;
      th.$api
        .updateMedicalDrugStatus({ medicalDrugId: obj.id, status: obj.status })
        .then(res => {
          if (res.executed) {
            th.$message.success("修改成功");
            th.getData();
          }
        });
    },
    //获取详情
    getDetail(obj) {
      let th = this;
      let j = {};
      if (obj != null) {
        j = {
          id: th.jiaMi(obj.patientId),
          type: 2
        };
      } else {
        j = {
          id: th.jiaMi(0)
        };
      }
      th.$router.push({
        path: "/page/userDetail",
        query: th.getUrlToken(j)
      });
    },
    exportExcel() {
      let th = this;
      th.$api
        .selectLevelEnumList({
          hpId: th.adminUser.hpId,
          pageSize: "1000",
          pageNum: "1"
        })
        .then(res => {
          if (res.executed) {
            if (res.dataBody != undefined) {
              th.dataList = res.dataBody.list;
              Excel(
                th.dataList,
                [
                  { key: "name", name: "姓名" },
                  { key: "mobile", name: "手机号" },
                  { key: "levelName", name: "会员等级" },
                  { key: "theCcumulative", name: "累计消费" },
                  { key: "patientMoney", name: "储值金额" },
                  { key: "storedValue", name: "累计储值" },
                  { key: "levelTime", name: "到期日期" },
                  { key: "patientScore", name: "积分" }
                ],
                "会员列表"
              );
            }
          }
          th.showLoading(false);
        });
    }
  },
  components: {
    set: () => import("./_com/setVip.vue"),
    pay: () => import("./_com/pay.vue"),
    refund: () => import("./_com/refund.vue")
  }
};
</script>
<style lang="scss" scoped>
@import url("./_css/detail.scss");
</style>

